package by.bsu.scs_bsu.custom_tags;

import java.io.IOException;

import javax.servlet.jsp.tagext.TagSupport;

/*
 * To use tag insert <ctag:upload_form type="***"/> into your HTML
 * and replace "***" with attribute: "part" (only fields will be generated) 
 * or "full" (full form will be generated).
 * 
 */

public class UploadFormTag extends TagSupport {
	private static final long serialVersionUID = 1L;

	private static final String INDEPENDENT_FORM = "<form action=\"Controller?command=logout\" method=\"post\" enctype=\"multipart/form-data\">\n"
			+ "  <input name=\"upload_file\" type=\"file\"><br/>\n"
			+ "  <input type=\"submit\" value=\"Send file\"><br/>\n" 
			+ "</form>";

	private static final String INTEGRATION_FORM = "<input name=\"upload_file\" type=\"file\"><br/>";

	private static final String INDEPENDENT_ATR = "full";
	private static final String INTEGRATION_ATR = "part";
	private static final String ERROR_MESSAGE = "Unknown type!";

	private String type;

	public void setType(String type) {
		this.type = type;
	}

	public int doStartTag() {
		try {
			if (INDEPENDENT_ATR.equalsIgnoreCase(type)) {
				pageContext.getOut().write(INDEPENDENT_FORM);
			} else if (INTEGRATION_ATR.equalsIgnoreCase(type)) {
				pageContext.getOut().write(INTEGRATION_FORM);
			} else {
				pageContext.getOut().write(ERROR_MESSAGE);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}
}